package net.trevize.datasetexplorer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JMenuItem;

import net.trevize.galatee.GItem;
import net.trevize.galatee.Galatee;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * PopupMenuInitializer.java - Aug 3, 2010
 */

public class GalateePopupMenuInitializer {

	private static ArrayList<DatasetExplorerPlugin> plugins = new ArrayList<DatasetExplorerPlugin>();

	public static void initialize(final Galatee galatee,
			final DatasetExplorer dataset_explorer) {
		/*
		 * add a menuitem in the popupmenu of galatee for displaying image info
		 * using ImageMagick if we are using an UNIX platform.
		 */
		if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
			JMenuItem menu_item = new JMenuItem("Image info");
			galatee.getPopup_menu().add(menu_item);
			menu_item.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					final GItem gitem = (GItem) galatee.getTable().getValueAt(
							galatee.getTable().getSelectedRow(),
							galatee.getTable().getSelectedColumn());
					new Thread() {
						@Override
						public void run() {
							new ImageInfoFrame(gitem.getLocalFilepath());
						}
					}.start();
				}
			});
		}

		for (DatasetExplorerPlugin plugin : plugins) {
			plugin.updateGalateePopupMenu(galatee);
		}
	}

	public static void addPlugin(DatasetExplorerPlugin plugin) {
		plugins.add(plugin);
	}

}
